/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.converters;

import com.bionic.ce.entitybeans.currency.CurrencyFacadeLocal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

@FacesConverter("com.bionic.ce.CurrencyConverter")
@EJB(name = "CurrencyFacadeLoc", beanInterface = CurrencyFacadeLocal.class)

// Класс-конвертер для преобразования вадют
public class CurrencyConverter implements Converter{
private CurrencyFacadeLocal currencyEJB = null;
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        currencyEJB = lookupCurrencysFacadeLocal();
        return currencyEJB.findByShortName(value);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
       return value.toString();
    }
    private CurrencyFacadeLocal lookupCurrencysFacadeLocal() {
        try {
            Context c = new InitialContext();
            return (CurrencyFacadeLocal) c.lookup("java:comp/env/CurrencyFacadeLoc");
        } catch (NamingException ex) {
            Logger.getLogger(CurrencyConverter.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException(ex);
        }
    }
    
}
